<?php

class BackendController extends Controller
{
    public $hederMainLinks=array();
    public static $hederMainLinksArray=array(
        array(
            'id'=>'',
            'class'=>'',
            'url'=>'admin/index',
            'name'=>'main',
            'subMenu'=>array(
                array(
                    'url'=>'admin/link1',
                    'name'=>'link1',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link2',
                    'name'=>'link2',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link3',
                    'name'=>'link3',
                    'class'=>'',
                ),
            ),
        ),
        array(
            'id'=>'',
            'class'=>'',
            'url'=>'admin/content',
            'name'=>'content',
            'subMenu'=>array(
                array(
                    'url'=>'slider',
                    'name'=>'slider',
                    'class'=>'',
                ),
            ),
        ),
        array(
            'id'=>'',
            'class'=>'',
            'url'=>'admin/articles',
            'name'=>'articles',
            'subMenu'=>array(
                array(
                    'url'=>'admin/link1',
                    'name'=>'link1',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link2',
                    'name'=>'link2',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link3',
                    'name'=>'link3',
                    'class'=>'',
                ),
            ),
        ),
        array(
            'id'=>'',
            'class'=>'',
            'url'=>'admin/files',
            'name'=>'files',
            'subMenu'=>array(
                array(
                    'url'=>'admin/link1',
                    'name'=>'link1',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link2',
                    'name'=>'link2',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link3',
                    'name'=>'link3',
                    'class'=>'',
                ),
            ),
        ),
        array(
            'id'=>'',
            'class'=>'',
            'url'=>'admin/profile',
            'name'=>'profile',
            'subMenu'=>array(
                array(
                    'url'=>'admin/link1',
                    'name'=>'link1',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link2',
                    'name'=>'link2',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link3',
                    'name'=>'link3',
                    'class'=>'',
                ),
            ),
        ),
        array(
            'id'=>'',
            'class'=>'',
            'url'=>'admin/settings',
            'name'=>'settings',
            'subMenu'=>array(
                array(
                    'url'=>'index',
                    'name'=>'general',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link2',
                    'name'=>'link2',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link3',
                    'name'=>'link3',
                    'class'=>'',
                ),
            ),
        ),
        array(
            'id'=>'',
            'class'=>'',
            'url'=>'admin/user/management',
            'name'=>'users',
            'subMenu'=>array(
                array(
                    'url'=>'admin/link1',
                    'name'=>'link1',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link2',
                    'name'=>'link2',
                    'class'=>'',
                ),
                array(
                    'url'=>'admin/link3',
                    'name'=>'link3',
                    'class'=>'',
                ),
            ),
        ),
    );

    public $subnavLinks=array();

    public $dashboardIcons=array();
    public static $dashboardIconsArray=array(
        array(
            'class'=>'',
            'url'=>'admin/article',
            'alt'=>'write',
            'img'=>'Crystal_Clear_write.gif',
            'name'=>'new_article',
        ),
        array(
            'class'=>'',
            'url'=>'admin/uploadFile',
            'alt'=>'upload',
            'img'=>'Crystal_Clear_file.gif',
            'name'=>'upload_file',
        ),
        array(
            'class'=>'',
            'url'=>'admin/articles',
            'alt'=>'articles',
            'img'=>'Crystal_Clear_files.gif',
            'name'=>'articles',
        ),
        array(
            'class'=>'',
            'url'=>'admin/content',
            'alt'=>'content',
            'img'=>'Crystal_Clear_content.gif',
            'name'=>'content',
        ),
        array(
            'class'=>'',
            'url'=>'admin/calendar',
            'alt'=>'calendar',
            'img'=>'Crystal_Clear_calendar.gif',
            'name'=>'calendar',
        ),
        array(
            'class'=>'',
            'url'=>'admin/profile',
            'alt'=>'profile',
            'img'=>'Crystal_Clear_user.gif',
            'name'=>'my_profile',
        ),
        array(
            'class'=>'',
            'url'=>'admin/stats',
            'alt'=>'stats',
            'img'=>'Crystal_Clear_stats.gif',
            'name'=>'stats',
        ),
        array(
            'class'=>'',
            'url'=>'admin/settings',
            'alt'=>'settings',
            'img'=>'Crystal_Clear_settings.gif',
            'name'=>'settings',
        ),
        array(
            'class'=>'',
            'url'=>'admin/user/management',
            'alt'=>'users',
            'img'=>'Crystal_Clear_users.gif',
            'name'=>'users',
        ),
    );

    public $categoryImage;

    public function init()
    {

        Yii::app()->theme='backend';
        Yii::app()->language='ru';

        $currentRoute=Yii::app()->request->route;

        foreach(self::$hederMainLinksArray as $array){
            if(strpos($currentRoute,$array['url'])!==false){
                $this->subnavLinks=$this->createSubMenu($array['subMenu'],$array['url'],$currentRoute);
                $array['id']=' id="current"';
            }

            $name=$array['name'];
            $array['name']=Yii::t('menu',$array['name']);
            $array['url']=Yii::app()->createAbsoluteUrl($array['url']);

            $this->hederMainLinks[$name]=(object)$array;
        }

        foreach(self::$dashboardIconsArray as $array){
            $array['url']=Yii::app()->createAbsoluteUrl($array['url']);
            $array['img']=Yii::app()->url->theme.'image/dashboardIcons/'.$array['img'];
            $array['name']=Yii::t('menu',$array['name']);
            $this->dashboardIcons[]=(object)$array;
        }

        parent::init();

        if(!Yii::app()->user->checkAccess('admin')){
            $loginUrl=Yii::app()->user->loginUrl[0];
            if(Yii::app()->request->route!=$loginUrl) $this->redirect(Yii::app()->createUrl($loginUrl));
        }
    }

    protected function createSubMenu($menuArray,$url,$currentRoute='')
    {
        $subNavLinks=array();
        if(!$currentRoute) $currentRoute=Yii::app()->request->route;

        foreach($menuArray as $menu){

            if(strpos($currentRoute,$menu['url'])!==false){
                $menu['class'].=$menu['class'] ? ' active-link' : 'active-link';
            }

            if($menu['url']=='slider') $menu['url']='slider/index';
            else $menu['url'].='/'.$url;

            $menu['name']=Yii::t('menu',$menu['name']);
            $menu['url']=Yii::app()->createUrl($menu['url']);



            $subNavLinks[]=(object)$menu;
        }

        return $subNavLinks;
    }

    /*
    public function filters()
    {
        return array(
            'accessControl',
        );
    }


    public function accessRules()
    {
        return array(
            array(
                'allow',
                'roles'=>array('admin'),
            ),
            array(
                'allow',
                'actions'=>array('login'),
                'users'=>array('*'),
            ),
            array(
                'deny',
                'users'=>array('*'),
            ),
        );
    }
    */
}